home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / TypeInt.java < prev    next >
Text File  |  1997-06-09  |  3KB  |  155 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. /**
  18.  * Representation of SNMP INTEGER type.
  19.  * @see Type
  20.  * @version     $Id: TypeInt.java,v 1.5 1997/05/18 08:11:54 alex Exp $
  21.  * @author      Alex Kowalenko
  22.  */
  23.  
  24. public class TypeInt extends Type {
  25.   
  26.     static byte asnValue = ASN.INTEGER;
  27.     static String name = "INTEGER";
  28.  
  29.   private int _value;
  30.  
  31. /**
  32.  * Constructor
  33.  */    
  34.  
  35.     TypeInt() {
  36.     _value = 0;
  37.     }
  38.  
  39. /**
  40.  * Constructor from integer
  41.  */
  42.  
  43.     TypeInt(int value) {
  44.     _value = value;
  45.     }
  46.  
  47. /**
  48.  * Constructor from ByteBuffer
  49.  */
  50.  
  51.     TypeInt(ByteBuffer buf) {
  52.     int length = ASN.getLength(buf);
  53.     _value = 0;    
  54.     int i = -1;
  55.     int len = length;
  56.     while(len> 0) {
  57.         i++; len--;
  58.         _value <<=  8;
  59.         short temp = (byte) buf.byteAt(i);
  60.         if(temp < 0)
  61.         temp += 256;
  62.         _value += temp;
  63.     };
  64.     buf.removeBeginning(length);
  65.     return;
  66.     };
  67.  
  68. /**
  69.  * returns name of Type
  70.  */
  71.  
  72.     String typeName() {
  73.     return name;
  74.     };
  75.   
  76. /**
  77.  * Will read a value for the variable from str
  78.  */
  79.  
  80.     boolean read(String str) {
  81.     _value = Integer.valueOf(str).intValue();
  82.     return true;
  83.     };
  84.  
  85. /** 
  86.  * Get the integer value
  87.  */
  88.  
  89.     int value() {
  90.     return _value;
  91.     }
  92.  
  93. /**
  94.  * SNMP protocol conversion.  Convert the variable to a sequence of 
  95.  * bytes, according to SNMP Protocol rules.
  96.  */
  97.  
  98.     public ByteBuffer BERSerialize() {
  99.     ByteBuffer buffer = new ByteBuffer();
  100.     byte lenght = 4;
  101.     int integer = _value; 
  102.     long mask = 0x1FF << 24;
  103.     while((((integer & mask) == 0) || ((integer & mask ) == mask)) 
  104.           && lenght > 1 ) {
  105.         integer  <<= 8;
  106.         lenght --;
  107.     }
  108.     buffer.append(asnValue);
  109.     buffer.append(lenght);
  110.     mask = 0xFF << 24;
  111.     while((lenght--) > 0) {
  112.         buffer.append( (byte) ((integer & mask ) >> 24));
  113.         integer <<= 8;
  114.     };
  115.     return buffer;
  116.     };
  117.  
  118. /**
  119.  * Return a String representation of the type
  120.  */
  121.  
  122.   public String toString() {
  123.       return Integer.toString(_value);
  124.   };
  125.  
  126. /**
  127.  * Test Harness 
  128.  */
  129.     
  130.     static void testInt(TypeInt t) {
  131.     System.out.println("value is " + t );
  132.     StringUtil.Hexdump(t.BERSerialize());
  133.     ByteBuffer b = t.BERSerialize();
  134.     b.removeBeginning(1);
  135.     TypeInt newT = new TypeInt(b);
  136.     System.out.println("reserialise type is " + newT);
  137.     };
  138.  
  139. /**
  140.  * Test Harness 
  141.  */
  142.     
  143.   public static void main(String args[]) {
  144.       TypeInt t = new TypeInt(6);
  145.       testInt(t);
  146.       t = new TypeInt(314);
  147.       testInt(t);
  148.       t = new TypeInt(3141);  
  149.       testInt(t);
  150.       t = new TypeInt(314159);  
  151.       testInt(t);
  152.   };
  153.  
  154. };
  155.